package com.hand.exam.interceptor;

import com.alibaba.fastjson.JSON;
import com.hand.exam.annoction.PermissionAnnoction;
import com.hand.exam.constant.ErrorMsg;
import com.hand.exam.domain.User;
import com.hand.exam.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;


/**
 * @author 马旭阳
 * @date2019/7/17
 */
@Component
public class Controllerinterceptor implements HandlerInterceptor {

    @Autowired
    private IUserService userService;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod)handler;
        Method method = handlerMethod.getMethod();
        Annotation methodAnnotation = method.getAnnotation(PermissionAnnoction.class);
        String userId = request.getHeader("id");
        if (!StringUtils.isEmpty(userId)) {
            User user = userService.queryUserById(Long.valueOf(userId));
            if (user.getRoleId() == 2) {
                if (!StringUtils.isEmpty(methodAnnotation)) {
                    return true;
                } else if (!StringUtils.isEmpty(method.getDeclaringClass().getAnnotation(PermissionAnnoction.class))) {
                        return true;
                    }
                    response.setCharacterEncoding("UTF-8");
                    response.setContentType("application/json");
                    response.setStatus(HttpStatus.FORBIDDEN.value());
                    ErrorMsg errorMsg = new ErrorMsg("no no no", "没有权限");
                    PrintWriter printWriter = response.getWriter();
                    String msgJs = JSON.toJSONString(errorMsg);
                    printWriter.print(msgJs);
                    printWriter.close();
                    return false;
                }

            }
        return true;
  }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("Controllerinterceptor:postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("Controllerinterceptor:afterCompletion");
    }

//    private boolean isValidate(Object handler,HttpServletRequest request){
//        HandlerMethod handlerMethod = (HandlerMethod)handler;
//        Method method = handlerMethod.getMethod();
//        System.out.println("aaaa");
//        Annotation methodAnnotation = method.getAnnotation(PermissionAnnoction.class);
//        String userId = request.getHeader("userId");
//        System.out.println(userId);
//        if (!StringUtils.isEmpty(userId)) {
//            User user = userService.queryUserById(Long.valueOf(userId));
//            if (user.getRoleId() == 2) {
//                if (!StringUtils.isEmpty(methodAnnotation)) {
//                    return true;
//                } else {
//                    if (!StringUtils.isEmpty(method.getDeclaringClass().getAnnotation(PermissionAnnoction.class))) {
//                        return true;
//                    }
//                    return false;
//                }
//
//            }
//
//
//        }
//        return true;
//    }
}
